<?php


namespace app\api\controller\xilujob;

use app\common\controller\xilujob\XilujobApi;
use app\common\model\xilujob\CompanyProfile;
use app\common\model\xilujob\EnterpriseInfo;

use app\common\model\xilujob\XilujobUser;
use fast\Random;
use think\Exception;
use function fast\array_get;

class Enterprise extends XilujobApi {

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $restrictedActions = ['*'];

    /**
     * 获取enterprise info以及认证状态
     */
    public function info() {
        $user = $this->auth->getUser();
        $xilujobuser = XilujobUser::get(['user_id'=>$user->id]);
        $enterpriseModel = $xilujobuser->enterprise_model;
        $enterpriseInfo = $xilujobuser->enterpriseinfo;
        $enterpriseInfo->benefits;
        $this->success('',[
            'user'       => ['id'=>$user['id'],'mobile'=>$user['mobile']],
            'enterprise_info' => $enterpriseInfo,
            'audit_status'=>$enterpriseModel->audit_status,
            'personal_audit_status'=>$enterpriseModel->personal_audit_status,
            'fav_area_name' =>  $enterpriseInfo->city->name ?? '',
        ]);
    }

    /**
     * 企业认证
     */
    public function audit() {
        $info = $this->request->post();
        $xilujobuser = XilujobUser::get(['user_id'=>$this->auth->id]);

        if(empty($info['full_name'])){
            $info['full_name'] = $info['company_name'];
        }else if(empty($info['company_name'])){
            $info['company_name'] = $info['full_name'];
        }
        try {
            $ret = EnterpriseInfo::submitEnterpriseAuditInfo($xilujobuser->enterprise_model, $info);
            $ret = EnterpriseInfo::saveInfo($xilujobuser->enterprise_model->id, $info);
        }catch (Exception $e){
            $this->error($e->getMessage());
        }

        $auditStatus = $xilujobuser->enterprise_model->audit_status;
        $this->success('', ['enterprise_info'=>$ret, 'audit_status'=>$auditStatus]);
    }

}